import java.sql.*;

public class javaLeann {
    private String user;
    private String password;
    private String db;
    private Connection connection;
    public javaLeann(){

    }
    public javaLeann(String user,String password,String db){
        this.user=user;
        this.password=password;
        this.db=db;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
                    ""+db+"?useSSL=false&serverTimezone=UTC", this.user, this.password);
//            new JdbcLearn();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
    public void add(String sql) throws SQLException {
        new javaLeann();
        PreparedStatement Statement = connection.prepareStatement(sql);
        Statement.setString(1,"ee");
        Statement.setInt(2,24);
        Statement.setString(3,"男");
        Statement.setString(4,"汉语言");
        Statement.setDouble(5,80.9);
        int i = Statement.executeUpdate();
        System.out.println("数据库添加了"+i+"信息");
        Statement.close();
        connection.close();
    }
    public void delete(String sql) throws SQLException {
        new javaLeann();
        PreparedStatement Statement = connection.prepareStatement(sql);
        Statement.setString(1,"cc");
        int i = Statement.executeUpdate();
        System.out.println("数据库删除了"+i+"信息");
        Statement.close();
        connection.close();
    }
    public void update(String sql) throws SQLException {
        new javaLeann();
        PreparedStatement Statement = connection.prepareStatement(sql);
        Statement.setInt(1,44);
        Statement.setString(2,"aa");
        int i = Statement.executeUpdate();
        System.out.println("数据库修改了"+i+"信息");
        Statement.close();
        connection.close();
    }
    public void select(String sql) throws SQLException {
        new javaLeann();
        PreparedStatement Statement = connection.prepareStatement(sql);
        ResultSet resultSet = Statement.executeQuery();
        while (resultSet.next()) {
            System.out.println("name:" + resultSet.getString("name"));
            System.out.println("age:" + resultSet.getInt("age"));
            System.out.println("sex: " + resultSet.getString("sex"));
            System.out.println("major: " + resultSet.getString("major"));
            System.out.println("score: " + resultSet.getDouble("score"));
        }
        Statement.close();
        connection.close();
    }
}
